<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>First Consult</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>

<body>


  <div id="middlePan">
		<div id="middlebodyPan">
			<div class="specializationlist">
				<section>
	 	         	<br><h1>Search for Posts </h1><br>
	 	         	<div id='search-box'>
					  <form action='SearchPosts' id='search-form' method='get'>
					    <input id='search-text' name='query' placeholder='Search by Post label.' type='text' required title="Please enter the search data"/>

		 
		    <button id='search-button' type='submit'><span>Search</span></button>
					  </form>
					</div><br>
					</div>
					</section>
<br>
<% if(request!= null &&  request.getAttribute("isEmptyList")!=null && request.getAttribute("isEmptyList").toString().equals("true")) { %>
<c:if test="${  empty articleList}">


    <p> <b>There are no such Posts!. Be the first to post such question </b></p>
       
</c:if>
<% } %>
 <% String prevLabel= "" ; String currLabel="";

 %>
		<c:set var="prevLabel" value=""> </c:set>
        <c:forEach var="article" items="${articleList}">
        <div style="background-color: #EFEDD8; margin-top: 10px;" >
                    <table id="article" style="width:100%;">
               <c:set var="currLabel" value="${article.qid}"> </c:set>
               
               <c:choose>
               <c:when test="${currLabel!=prevLabel}">
                    <% System.out.println("Inside if"); %>        
            		<c:set var="prevLabel" value="${article.qid}"> </c:set>
            		<tr><td><b>${article.qlabel}</b></td></tr>
                	<tr><td>&nbsp;&nbsp;&nbsp;${article.question}</td></tr>
                	<tr><td>&nbsp;&nbsp;<b>by</b>   ${article.user}</td></tr>
                	<tr><td>&nbsp;&nbsp;<b>on</b>   ${article.date}</td></tr>
                	
                	
               </c:when>
               <c:otherwise>
               	<% System.out.println("Inside else"); %>
               		
              <tr><td><b>Comments </b>: ${article.answer}</td></tr>
               </c:otherwise>
               </c:choose>  
                            
          </table>
          </div>
        </c:forEach>
  
    
<br>
  <div align="center">
    <%--For displaying Previous link except for the 1st page --%>
    <c:if test="${currentPage != 1 and currentPage !=null }">
        <td><a href="SearchPosts?query=<%= request.getParameter("query")%>&page=${currentPage - 1}">Previous</a></td>
    </c:if>
  </div>
    <%--For displaying Page numbers.
    The when condition does not display a link for the current page--%>
    <div align = "center" >
    <table align="center" border="1" cellpadding="5" cellspacing="5">
        <tr>
            <c:forEach begin="1" end="${noOfPages}" var="i">
                <c:choose>
                    <c:when test="${currentPage eq i}">
                        <td>${i}</td>
                    </c:when>
                    <c:otherwise>
                        <td><a href="SearchPosts?query=<%= request.getParameter("query")%>&page=${i}">${i}</a></td>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
        </tr>
    </table>
 </div>
 <div align="center">
    <%--For displaying Next link --%>
    <c:if test="${currentPage lt noOfPages}">
        <td><a href="SearchPosts?query=<%= request.getParameter("query")%>&page=${currentPage + 1}">Next</a></td>
    </c:if>
 </div>
		
			</div>
		</div>
	
		

</body>
</html>